//
//  StatusCellPictureView.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/19.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit
import SDWebImage

private let pictureCellId = "pictureCellId"
private let pictureCellMargin : CGFloat = 5
class StatusCellPictureView: UICollectionView {
    var imageURLs : [NSURL]? {
        didSet{
            let pSize = caclePictureViewSize()
            self.snp_updateConstraints { (make) -> Void in
                 make.size.equalTo(pSize)
            }
            testLabel.text = "\(imageURLs?.count ?? 0)"
            reloadData()
        }
    }
    private func caclePictureViewSize() -> CGSize {
        let imageCount = imageURLs?.count ?? 0
        if imageCount == 0 {
            return CGSizeZero
        }
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if imageCount == 1 {
            let itemSize = CGSize(width: 180, height: 120)
            layout.itemSize = itemSize
            return itemSize
        }
        
        let itemWidth = (KWidth - 2 * StatusCellMargin - 2 * pictureCellMargin) / 3
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        if imageCount == 4 {
            let w = 2 * itemWidth + pictureCellMargin
            return CGSizeMake(w, w)
        }
        
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let maxW = itemWidth * 3  + 2 * pictureCellMargin
        let maxH = itemWidth * row + pictureCellMargin * (row - 1)
    
        return CGSizeMake(maxW, maxH)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = pictureCellMargin
        layout.minimumLineSpacing = pictureCellMargin
        super.init(frame: frame, collectionViewLayout: layout)
        self.backgroundColor = UIColor.whiteColor()
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    private func setupUI(){
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        self.dataSource = self
        
    }
    
    private lazy var testLabel : UILabel = UILabel(title: "", fontSize: 40, color: UIColor.redColor())
    

}

extension StatusCellPictureView : UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! PictureCell
        cell.imageURL = imageURLs![indexPath.item]
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.orangeColor() : UIColor.greenColor()
        return cell
    }
}

class PictureCell : UICollectionViewCell {
    var imageURL : NSURL? {
        didSet{
            iconView.sd_setImageWithURL(imageURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    private func setupUI() {
        contentView.addSubview(iconView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    private lazy var iconView : UIImageView = {
       
        let iv = UIImageView()
        iv.contentMode = .ScaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()

}

